<template>
  <div class="container">
    <div class="topbar">
      <div class="topbar-section">
        <span class="topbar-logo"><img src="../../.././assets/img/logo.png" width="100px"></span>
      </div>
      <div class="topbar-section">
        <div class="topbar-product">
          <div class="topbar-btn">
            <span class="avatar"><img :src="user.avatar"></span>
            <span class="topbar-nickname">{{user.realName}}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="full-framework">
      <div class="sidebar">
        <div class="title">运营中心</div>
        <el-menu
          :default-active="activeUrl"
          class="el-menu-vertical"
          background-color="#F5F5F5"
          text-color="#555555"
          unique-opened
          router>
          <div v-for="menu in menuList" :key="menu.name">
            <el-submenu :index="menu.index + ''" v-if="menu.child">
              <template slot="title">{{menu.name}}</template>
              <el-menu-item v-for="submenu in menu.child" :key="submenu.childIdx" :index="submenu.url" :class="{active : submenu.url === activeUrl}">{{submenu.childName}}</el-menu-item>
            </el-submenu>
            <el-menu-item :index="menu.url" v-else :class="{active : menu.url === activeUrl}">{{menu.name}}</el-menu-item>
          </div>
        </el-menu>
      </div>
      <div class="content">
        <keep-alive>
          <router-view ref="component" v-if="hasLimit && $route.meta.keepAlive"></router-view>
        </keep-alive>
        <router-view v-if="hasLimit && !$route.meta.keepAlive"></router-view>
        <h1 v-if="!hasLimit" style="position: absolute; text-align: center; padding: 15px;">您没有查看权限</h1>
      </div>
    </div>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
export default {
  data () {
    return {
      menuList: [{
        index: 1, name: '概览', url: '/bms-statistics'
      },{
        index: 2, name: '设备运维', child: [{childIdx: 1, childName: '设备列表', url: '/bms-switchList'}, {childIdx: 2, childName: '活跃设备', url: '/bms-switchActive'}, {childIdx: 3, childName: '新增设备', url: '/bms-newSwitch'}]
      },{
        index: 3, name: '用户运营', child: [{childIdx: 1, childName: '用户列表', url: '/bms-userList'}, {childIdx: 2, childName: '活跃用户', url: '/bms-userActive'}, {childIdx: 3, childName: '新增用户', url: '/bms-newUser'}, {childIdx: 4, childName: '用户反馈', url: '/bms-userFeedback'}]
      },{
        index: 4, name: '邮件', child: [{childIdx: 1, childName: '邮件列表', url: '/bms-emailList'}, {childIdx: 2, childName: '草稿箱', url: '/bms-messageList'}, {childIdx: 3, childName: '发件人列表', url: '/bms-senderList'}]
      }],
      activeUrl: '/bms-statistics',
      user: {},
    };
  },
  watch: {
    $route (to, from) {
      this.activeUrl = to.path
      if(to.path==='/bms-switchDetail') this.activeUrl = '/bms-switchList'
      if(to.path==='/bms-userDetail') this.activeUrl = '/bms-userList'
      window.NEEDREFRESH = from.path !== '/bms-switchDetail' && from.path !== '/bms-userDetail' ? true : false;
    }
  },
  computed: {
    isManager() {
      return this.userInfo.roleIdList && this.userInfo.roleIdList.includes('1311210362356432897')
    },
    hasLimit() {
      return this.userInfo.roleIdList && (this.userInfo.roleIdList.includes('1311210362356432897') || this.userInfo.roleIdList.includes('1311210442408919042') || this.userInfo.roleIdList.includes('1328502655740379138'))
    }
  },
  mixins: [dingMixin],
  mounted () {
    this.getDingConfig().then(config =>{
      this.dingConfig = config;
      this.dingLogin(config.corpId, () => {
        if(this.isManager) {
          this.menuList.push({
            index: 5,
            name: '权限设置',
            url: '/bms-setting'
          });
        }
        this.user = this.userInfo
        this.$router.push({ name: 'bms-statistics'})
      });
      this.run_dd_config();
    })
  },
  methods: {
    
  }
}
</script>

<style lang='scss' scoped>
.container {
  width: 100%;
  height: 100vh;
  .topbar {
    width: 100%;
    height: 50px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    display: flex;
    justify-content: space-between;
    .topbar-section {
      display: flex;
      .topbar-logo {
        line-height: 50px;
        padding: 0 16px;
        cursor: pointer;
        display: flex;
        align-items: center;
      }
      .topbar-product {
        position: relative;
        .topbar-btn {
          padding: 0 24px;
          height: 50px;
          line-height: 50px;
          font-size: 14px;
          color: #333;
          display: inline-block;
          .avatar {
            img {
              width: 30px;
              height: 30px;
              border-radius: 50%;
            }
          }
          .topbar-nickname {
            padding: 0 6px;
            display: inline-block;
          }
        }
        .el-icon-caret-bottom {
          color: #888;
          display: inline-block;
          width: 18px;
          text-align: center;
          vertical-align: middle;
        }
        .topbar-dropdown {
          position: absolute;
          left: 0;
          right: 0;
          top: 50px;
          display: none;
          z-index: 1;
          box-shadow: 0 1px 3px rgba(0,0,0,0.2);
          .dropdown-style {
            max-height: 400px;
            font-size: 12px;
            color: #373D41;
            line-height: 32px;
            background-color: #FFF;
            overflow: auto;
            padding-top: 8px;
            div {
              line-height: 20px;
              cursor: pointer;
              padding: 6px 12px;
              &:hover {
                color: #FF6A00;
              }
            }
          }
        }
        &:hover .topbar-dropdown {
          display: block;
        }
      }
    }
  }
  .full-framework {
    width: 100%;
    height: calc(100% - 50px);
    display: flex;
    .sidebar {
      width: 208px;
      height: 100%;
      background-color: rgb(245, 245, 245);
      .title {
        height: 45px;
        line-height: 45px;
        font-weight: bold;
        padding: 0 16px;
        color: #333;
        border-bottom: 1px solid #dedede;
      }
      .el-menu {
        border: none;
      }
      .el-menu-item, .el-menu-vertical /deep/ .el-submenu__title{
        height: 40px;
        line-height: 40px;
      }
      .active {
        font-weight: bold;
        background-color: #DEDEDE !important;
        color: #303133;
      }
    }
    .content {
      width: 100%;
      flex: 1;
      overflow: auto;
    }
  }
}
</style>